﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Web.Caching;
using System.Text;
using Dimok.Exceptions;
using Dimok;
using Dimok.Web;
using Dimok.Shared;
using Data.LINQ;

[ServiceContract(Namespace = "")]
public interface ISLService : IWcfGeneric {
	//[OperationContract]
	//v_Bridge[] GetBridge(string PageType, string ControlID, string ReportName,
	//  Dictionary<string, string> SortExpressions, Dictionary<string, string> Filters, out FaultContract Fault);
	[OperationContract]
	Data.LINQ.v_Bridge CollectionInfo_Vantage(string VantageID, out FaultContract Fault);
	[OperationContract]
	Data.LINQ.vantage_Note[] Notes_Vantage(string VantageID, out FaultContract Fault);
	[OperationContract]
	Data.LINQ.vantage_Payment[] Payments_Vantage(string VantageID, out FaultContract Fault);
	[OperationContract]
	Data.LINQ.PageCountsResult[] PageCounts(int LoanID, bool ExcludeEmpty, out FaultContract Fault);
	[OperationContract]
	void ReportError(string ErrorMessage);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SLService : Dimok.Web.WcfGeneric<MotoBank,Profiler,TablesMetaData>,ISLService{
	public v_Bridge[] GetBridge(string PageType, string ControlID, string ReportName,
		Dictionary<string, string> SortExpressions, Dictionary<string, string> Filters, out FaultContract Fault) {
		var ret = GetEntity<v_Bridge>(
			PageType, ControlID, ReportName, SortExpressions, Filters, 0, Filters.Trim().Count == 0 ? 0 : -1, out Fault);
		return ret.Length > 0 ? ret : new v_Bridge[] { new v_Bridge() };
	}
	public Data.LINQ.v_Bridge CollectionInfo_Vantage(string VantageID, out FaultContract Fault) {
		Fault = null;
		try {
			return new Data.LINQ.MotoBank().v_Bridges.Where(r => r.VantageID == VantageID).FirstOrDefault();
		} catch (Exception exc) {
			ASPException.Publish(exc);
			Fault = new FaultContract() { FaultType = exc.GetType().Name, Message = exc.Message };
			return null;
		}
	}
	public Data.LINQ.PageCountsResult[] PageCounts(int LoanID,bool ExcludeEmpty, out FaultContract Fault) {
		Fault = null;
		try {
			var q = new MotoBank().PageCounts(LoanID);
			if (ExcludeEmpty) q = q.Where(it => it.Count > 0);
			return q.OrderBy(it => it.TabIndex).ToArray();
		} catch (Exception exc) {
			ASPException.Publish(exc);
			Fault = new FaultContract() { FaultType = exc.GetType().Name, Message = exc.Message };
			return null;
		}
	}
	public Data.LINQ.vantage_Note[] Notes_Vantage(string VantageID, out FaultContract Fault) {
		Fault = null;
		try {
			return new MotoBank().vantage_Notes.Where(n=>n.VantageID== VantageID).OrderByDescending(n=>n.NoteDate).ToArray();
		} catch (Exception exc) {
			ASPException.Publish(exc);
			Fault = new FaultContract() { FaultType = exc.GetType().Name, Message = exc.Message };
			return null;
		}
	}
	public Data.LINQ.vantage_Payment[] Payments_Vantage(string VantageID, out FaultContract Fault) {
		Fault = null;
		try {
			return new MotoBank().vantage_Payments.Where(n => n.VantageID == VantageID).OrderByDescending(n => n.PaymentEffectiveDate).ToArray();
		} catch (Exception exc) {
			ASPException.Publish(exc);
			Fault = new FaultContract() { FaultType = exc.GetType().Name, Message = exc.Message };
			return null;
		}
	}
	public void ReportError(string ErrorMessage) {
		Dimok.Exceptions.ASPException.Publish(new Exception(ErrorMessage));
	}

	// Add more operations here and mark them with [OperationContract]
}
